﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages
{
    public class DiseaseStat : YouHP.Pages.BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int type = RequestTools.GetInt("type", 0);

            switch(type)
            {
            case 1:
                MakeDiseaseRegionGraph();
                break;

            case 2: 
                MakeDiseaseAgeGraph();
                break;

            case 3:
                MakeDiseaseDuration();
                break;

            case 4:
                MakeDiseaseStymptom();
                break;

            case 5:
                MakeDiseaseTreatment();
                break;

            default:
                break;
            }
        }

        private void MakeDiseaseRegionGraph()
        {
            int did = RequestTools.GetQueryInt("did", 0);
            int rid = RequestTools.GetQueryInt("rid", 0);

            if (did <= 0 ) 
                return;
            
            List<DiseaseStatRegionInfo> lst = DiseaseBLL.Instance.GetDiseaseStatRegion(did, rid);
            StringBuilder sb = new StringBuilder();
            
            sb.AppendLine("&Bar=1&\r\n");

            int i=0;
            foreach (DiseaseStatRegionInfo info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2}&\r\n", i, info.RegionName, info.PatientCount);
                i++;
            }

            sb.AppendFormat("&Var=疾病地区分布;{0}&;\r\n", lst.Count);
            
            ResponseXML(sb);
        }

        private void MakeDiseaseAgeGraph()
        {
            int did = RequestTools.GetQueryInt("did", 0);
            if (did <= 0) return;

            List<DiseaseStatAgeInfo> lst = DiseaseBLL.Instance.GetDiseaseStatAge(did);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Histogram=1&\r\n");
            int maxcnt = 0;
            int i = 0;
            foreach (DiseaseStatAgeInfo info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2}&\r\n", i, info.Age, info.PatientCount);
                i++;
                if (maxcnt < info.PatientCount) maxcnt = info.PatientCount;
            }
            sb.AppendFormat("&MaxValue={0}&;\r\n", maxcnt);
            sb.AppendFormat("&Var={0}&;\r\n", lst.Count);
            string url = this.GetUrl("/DiseaseStat.aspx?type=2&did=" + did + "&rid=");
            //sb.AppendFormat("&Link={0};\r\n", url);
            ResponseXML(sb);

        }

        private void MakeDiseaseDuration()
        {
            int did = RequestTools.GetQueryInt("did", 0);
            if (did <= 0) return;
            List<DiseaseStatDurationInfo> lst = DiseaseBLL.Instance.GetDiseaseStatDuration(did);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Histogram=1&\r\n");
            int maxcnt = 0;
            int i = 0;
            foreach (DiseaseStatDurationInfo info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, info.Duration, "", info.PatientCount);
                i++;
                if (maxcnt < info.PatientCount) maxcnt = info.PatientCount;
            }
            sb.AppendFormat("&MaxValue={0}&;\r\n", maxcnt);
            sb.AppendFormat("&Var={0}&;\r\n", lst.Count);
            string url = this.GetUrl("/DiseaseStat.aspx?type=2&did=" + did + "&rid=");
            //sb.AppendFormat("&Link={0};\r\n", url);
            ResponseXML(sb);

        }

        private void MakeDiseaseStymptom()
        {
            int did = RequestTools.GetQueryInt("did", 0);
            if (did <= 0) return;
            
            List<DiseaseStatSymptomInfo> lst = DiseaseBLL.Instance.GetDiseaseStatSymptom(did);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Bar=1&\r\n");

            int i = 0;
            string nameStr = "";
            string linkStr = "";

            foreach (DiseaseStatSymptomInfo info in lst)
            {
                nameStr = string.Format("<a href='{0}'>{1}</a>", GetUrl("/Symptom.aspx?id=" + info.SymptomId), info.SymptomName);
                if (info.PatientCount > 0)
                {
                    linkStr = GetUrl("/PatientList.aspx?symid=" + info.SymptomId);
                }
                else
                {
                    linkStr = "";
                }
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, nameStr, linkStr, info.PatientCount);
                i++;
            }
            sb.AppendFormat("&Var=疾病症状分布统计;{0}&;\r\n", lst.Count);

            ResponseXML(sb);
        }

        private void MakeDiseaseTreatment()
        {
            int did = RequestTools.GetQueryInt("did", 0);
            if (did <= 0) return;
            
            List<DiseaseStatTreamentInfo> lst = DiseaseBLL.Instance.GetDiseaseStatTreament(did);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Bar=1&\r\n");

            int i = 0;
            string nameStr = "";
            string linkStr = "";

            foreach (DiseaseStatTreamentInfo info in lst)
            {
                nameStr = string.Format("<a href='{0}'>{1}</a>", GetUrl("/Treatment.aspx?id=" + info.TreatmentId), info.TreatmentName);
                if (info.PatientCount > 0)
                {
                    linkStr = GetUrl("/PatientList.aspx?trmid=" + info.TreatmentId);
                }
                else
                {
                    linkStr = "";
                }
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, nameStr, linkStr, info.PatientCount);
                i++;
            }
            sb.AppendFormat("&Var=疾病治疗分布统计;{0}&;\r\n", lst.Count);
            
            ResponseXML(sb);
        }

        /*
       private void MakeWeightGraphData(PatientRecordInfo prData, List<WeightHistoryInfo> lst, double height)
       {
            StringBuilder sb = new StringBuilder();
            sb.Append("&SingleGeneral=1&\r\n");
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);
            string str1 = "&Time1=";
            string str2 = "&Value1=";
            string str3 = "&Time2=";
            string str4 = "&Value2=";
            int i = 0;
            for (i = lst.Count - 1; i >= 0; i--)
            {
                WeightHistoryInfo item = lst[i];
                if (i < lst.Count - 1)
                {
                    str1 += ";";
                    str2 += ";";
                    str3 += ";";
                    str4 += ";";
                }
                str1 += item.RecordDate.ToString("yyyy-MM-dd");
                double va = Math.Floor(item.DataValue * 100) / 100;
                str2 += va.ToString();
                str3 += item.RecordDate.ToString("yyyy-MM-dd");
                va = Math.Floor(item.DataValue / height / height * 100) / 100;
                str4 += va.ToString();
            }
            str1 += "&\r\n";
            str2 += "&\r\n";
            str3 += "&\r\n";
            str4 += "&\r\n";
            sb.Append(str1);
            sb.Append(str2);
            sb.Append(str3);
            sb.Append(str4);

            ResponseXML(sb);
        }
        */
        

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}